﻿#include "u_equipment_page.h"
#include "u_equipment_edit.h"

EquipmentPage::EquipmentPage(QWidget *parent) : MyDialog(parent)
{
    this->InitWidget();
    this->SetupConnections();
    this->Initialize();

    this->setMaxHide();
    this->setTitle("发射机管理");
    this->resize(1100, 600);
}

void EquipmentPage::InitWidget(){
    btnAdd  = new QPushButton(QChar(0xe722) + QString(" 添加"));
    btnDel  = new QPushButton(QChar(0xe603) + QString(" 删除"));

    QStringList headers = QString("设备代号,设备号,设备名称,串口号,波特率,数据位,校验位,停止位,入射系数,反射系数,高频系数,"
                                  "-140V系数,-18V系数,12V系数,18V系数,24V系数,音频系数,中放系数,调制系数,推动系数").split(",");
    tableWidget = MyGUI::GetTableWidget(headers, 120, false);
//    tableWidget->setColumnWidth(1, 200);
//    tableWidget->horizontalHeader()->setStretchLastSection(true);
//    tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addStretch();
    btnLayout->addWidget(btnAdd);
    btnLayout->addWidget(btnDel);

    QGroupBox *groupbox11 = new QGroupBox();
    groupbox11->setStyleSheet("margin-top: 0px;");
    groupbox11->setLayout(btnLayout);

    //-------------------------------------------------

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(10);
    if(globalData->CheckUserRole(0)){
        mainLayout->addWidget(groupbox11);
    }
    mainLayout->addWidget(tableWidget);
    this->addLayout(mainLayout);
}

void EquipmentPage::SetupConnections(){
    connect(btnAdd , SIGNAL(clicked()), this, SLOT(OnBtnAddClicked()));
    connect(btnDel , SIGNAL(clicked()), this, SLOT(OnBtnDelClicked()));
    connect(tableWidget,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(OnTableDoubleClicked(int,int)));
}

void EquipmentPage::Initialize(){
    queryList = globalData->sqlBase->GetEquipmentList();
    this->UpdateTable();
}

void EquipmentPage::UpdateTable(){
    int count = queryList.size();

    tableWidget->setRowCount(0);
    tableWidget->setRowCount(count);

    for(int i=0; i<count; i++){
        Equipment info = queryList.at(i);

        tableWidget->setItem(i, 0, MyGUI::GetTableWidgetItem(info.devCode));
        tableWidget->setItem(i, 1, MyGUI::GetTableWidgetItem(info.devid));
        tableWidget->setItem(i, 2, MyGUI::GetTableWidgetItem(info.devname));
        //tableWidget->setItem(i, 3, MyGUI::GetTableWidgetItem(info.type ? 50 : 10));
        //tableWidget->setItem(i, 4, MyGUI::GetTableWidgetItem(info.cosjx));
        tableWidget->setItem(i, 3, MyGUI::GetTableWidgetItem(info.serialName));
        tableWidget->setItem(i, 4, MyGUI::GetTableWidgetItem(info.baudRate));
        tableWidget->setItem(i, 5, MyGUI::GetTableWidgetItem(info.dataBit));
        tableWidget->setItem(i, 6, MyGUI::GetTableWidgetItem(getcheck(info.checkoutBit)));
        tableWidget->setItem(i, 7, MyGUI::GetTableWidgetItem(getstop(info.stopBit)));
        tableWidget->setItem(i, 8, MyGUI::GetTableWidgetItem(info.cosrs));
        tableWidget->setItem(i, 9, MyGUI::GetTableWidgetItem(info.cosfs));
        tableWidget->setItem(i, 10, MyGUI::GetTableWidgetItem(info.cosgp));
        tableWidget->setItem(i, 11, MyGUI::GetTableWidgetItem(info.cos_140));
        tableWidget->setItem(i, 12, MyGUI::GetTableWidgetItem(info.cos_18));
        tableWidget->setItem(i, 13, MyGUI::GetTableWidgetItem(info.cos_12));
        tableWidget->setItem(i, 14, MyGUI::GetTableWidgetItem(info.cos_z18));
        tableWidget->setItem(i, 15, MyGUI::GetTableWidgetItem(info.cos_24));
        tableWidget->setItem(i, 16, MyGUI::GetTableWidgetItem(info.cosyp));
        tableWidget->setItem(i, 17, MyGUI::GetTableWidgetItem(info.coszf));
        tableWidget->setItem(i, 18, MyGUI::GetTableWidgetItem(info.costz));
        tableWidget->setItem(i, 19, MyGUI::GetTableWidgetItem(info.costd));

        //tableWidget->setItem(i, 7, MyGUI::GetTableWidgetItem(info.enable ? "启用" : "禁用", Qt::AlignCenter, info.enable ? Qt::green : Qt::red));
    }
}

QString EquipmentPage::getcheck(int a)
{
    QString str;
    if(a==0){
        str ="N空";
    }
    else if(a == 1)
    {
        str =  "E偶";
    }
    else if(a == 2){
        str = "0奇";
    }
    else if(a == 3){
        str = "S空格";
    }
    else if(a == 4){
        str = "M标志";
    }
    return str;
}

float EquipmentPage::getstop(int a)
{
    float fl;
    if(a == 0)
    {
        fl=1;
    }
    else if(a == 1)
    {
        fl = 1.5;
    }
    else if(a == 2)
    {
        fl = 2;
    }
    return fl;
}

void EquipmentPage::OnBtnAddClicked(){
    Equipment info;
    (new EquipmentEdit(info, this))->exec();
    this->Initialize();
}

void EquipmentPage::OnBtnDelClicked(){
    int row = tableWidget->currentRow();
    if(row < 0){
        MyGUI::ShowMessageBoxError("请选择要删除的记录", this);
        return;
    }
    if(!MyGUI::ShowMessageBoxQuesion("确定要删除选中的记录？", this)){
        return;
    }

    Equipment info = queryList.at(row);
    bool b = globalData->sqlBase->DelEquipment(info.serialName);

    if(b){
        this->Initialize();
        MyGUI::ShowMessageBoxInfo("删除成功", this);
    }else{
        MyGUI::ShowMessageBoxError("删除失败", this);
    }
}

void EquipmentPage::OnTableDoubleClicked(int row, int column){
    Equipment info = queryList.at(row);
    (new EquipmentEdit(info, this))->exec();
    this->Initialize();
}
